from flask import Blueprint, request
from utilsapp.mysql_con import db_connect

region = Blueprint('region', __name__)
""" 
查询集团、公司、工厂、区域 列表
"""


# 获取集团列表
@region.route('/group/getlist')
def get_group_level():
    page = int(request.args.get('page'))
    size = int(request.args.get('size'))
    sql = f'SELECT * FROM regionlevel where type="group" limit {max(0, (page - 1) * size)},{size}'
    sql_1 = 'SELECT count(id) FROM regionlevel where type="group"'
    total_num = db_connect(sql_1)
    total = total_num[0]['count(id)']
    data = db_connect(sql)
   
    result = {
        'msg': '查询成功',
        'code': 0,
        'data': data,
        'pagination': {
            "page": page,
            "pageSize": size,
            "total": total
        }
    }
    return result


# 获取公司列表
@region.route('/company/getlist')
def get_company_level():
    page = int(request.args.get('page'))
    size = int(request.args.get('size'))
    sql = f"""SELECT a.id,a.name,b.name as groupName FROM regionlevel a left join regionlevel b on a.parent=b.id
    where a.type="company" limit {max(0, (page - 1) * size)},{size}"""
    sql_1 = 'SELECT count(id) FROM regionlevel where type="company"'
    total_num = db_connect(sql_1)
    total = total_num[0]['count(id)']
    data = db_connect(sql)
   
    result = {
        'msg': '查询成功',
        'code': 0,
        'data': data,
        'pagination': {
            "page": page,
            "pageSize": size,
            "total": total
        }
    }
    return result


# 查询工厂列表
@region.route('/factory/getlist')
def get_factory_list():
    page = int(request.args.get('page'))
    size = int(request.args.get('size'))
    sql = f"""SELECT a.id,a.name,b.name as companyName,c.name as groupName FROM
    regionlevel a left join regionlevel b on a.parent=b.id left join regionlevel c on b.parent=c.id
    where a.type="factory" limit {max(0, (page - 1) * size)},{size}"""
    sql_1 = 'SELECT count(id) FROM regionlevel where type="factory"'
    total_num = db_connect(sql_1)
    total = total_num[0]['count(id)']
    data = db_connect(sql)
    result = {
        'msg': '查询成功',
        'code': 0,
        'data': data,
        'pagination': {
            "page": page,
            "pageSize": size,
            "total": total
        }
    }
    return result


# 查询区域列表
@region.route('/zone/getlist')
def get_zone_list():
    page = int(request.args.get('page'))
    size = int(request.args.get('size'))
    sql = f"""SELECT a.id,a.name,b.name as factoryName,c.name as companyName,d.name as groupName FROM
    regionlevel a left join regionlevel b on a.parent=b.id left join regionlevel c on b.parent=c.id left join regionlevel d on c.parent=d.id
    where a.type="zone" limit {max(0, (page - 1) * size)},{size}"""
    sql_1 = 'SELECT count(id) FROM regionlevel where type="zone"'
    total_num = db_connect(sql_1)
    total = total_num[0]['count(id)']
    data = db_connect(sql)
   
    result = {
        'msg': '查询成功',
        'code': 0,
        'data': data,
        'pagination': {
            "page": page,
            "pageSize": size,
            "total": total
        }
    }
    return result
